package com.example.smartcitymovie.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.example.smartcitymovie.BR;
import com.example.smartcitymovie.R;

import java.util.List;

/**
 * @Author: Machu
 * @Time: 2022/2/14 13:27
 * @Description: 功能描述
 */
public class RecycleViewAdapter<T> extends RecyclerView.Adapter<BindingViewHolder> {

    private Context context;
    private List<T> list;
    private int resource;
    private LayoutInflater layoutInflater;
    private int partId = 0;
    private int size;

    public RecycleViewAdapter(Context context, List<T> list, int resource) {
        this.context = context;
        this.list = list;
        this.resource = resource;
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public BindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BindingViewHolder(DataBindingUtil.inflate(layoutInflater,resource,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull BindingViewHolder holder, @SuppressLint("RecyclerView") int position) {
        ViewDataBinding myBinding = holder.getMyBinding();
        myBinding.setVariable(BR.model,list.get(position));
        View root = myBinding.getRoot();
        myBinding.executePendingBindings();
        if (root!=null&&onRecycleViewItemCLickListener!=null){
            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onRecycleViewItemCLickListener.onRecycleViewItemCLickListener(root,position);
                }
            });
        }
        if (partId!=0&&onPartRecycleViewItemClickListener!=null){
            View partView = root.findViewById(partId);
            if (partView!=null){
                partView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onPartRecycleViewItemClickListener.onPartRecycleViewItemClickListener(root,position);
                    }
                });
            }
        }
    }



    @Override
    public int getItemCount() {
        if (list==null)
        {
            return 0;
        }
        return size == 0?list.size() :size;
    }
    public void setSize(){
        this.size = size;
    }

    public OnRecycleViewItemCLickListener onRecycleViewItemCLickListener;
    public OnPartRecycleViewItemClickListener onPartRecycleViewItemClickListener;


    public void setOnRecycleViewItemCLickListener(OnRecycleViewItemCLickListener onRecycleViewItemCLickListener){
        this.onRecycleViewItemCLickListener = onRecycleViewItemCLickListener;
    }
    public void setOnPartRecycleViewItemClickListener(OnPartRecycleViewItemClickListener onPartRecycleViewItemClickListener,int partId){
        this.onPartRecycleViewItemClickListener = onPartRecycleViewItemClickListener;
        this.partId =partId;
    }

    public interface OnRecycleViewItemCLickListener{
        void onRecycleViewItemCLickListener(View view,int position);
    }
    public interface OnPartRecycleViewItemClickListener{
        void onPartRecycleViewItemClickListener(View view,int position);
    }
}
